<cfcomponent output="false">

	<cffunction name="GetAllUsers" output="false" access="remote">
		<cfargument name="id" required="true" />
		<cfset var user = "" />
		<cfset var allusers = "" />
		<cfset var qRead = "" />
		
		<cfquery username="#request.username#" password="#request.password#" name="qRead" datasource="nwharvest">
			SELECT ID
			FROM dbo.USERS
		</cfquery>

		<cfloop query="qRead">
			<cfscript>
				user = createObject("component", "UserDAO").read(qRead.ID);
				ArrayAppend(allusers, user);
			</cfscript>
		</cfloop>
		
		<cfreturn allusers>
	</cffunction>		
	
	<cffunction name="saveUser" output="true" access="remote">
		<cfargument name="obj" required="true" />
		<cfset var user = "">
		<cfset var organization = "">
		<cfset var address = "">	
		<cfset var fooddrive = "">
	
			<cfif arguments.obj.ID eq 0>

				<!--- check if the user email is in use --->
				<cfquery username="#request.username#" password="#request.password#" name="emailCheck" datasource="nwharvest">
					select ID from Users
					where Email = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.obj.email#" />
				</cfquery>
	
				<cfset organization = createObject("component","Organization") />
				<cfset organization.ADMIN_ID = arguments.obj.ADMIN_ID />
				<cfset organization.ORG_NAME = arguments.obj.ORG_NAME />
				<cfset organization.ORG_CAT_ID = arguments.obj.ORG_CAT_ID />
				<cfset organization.LOGO_FILE = arguments.obj.LOGO_FILE />	

				<cfset address = createObject("component", "Address") />
				<cfset address.ADDRESS1 = arguments.obj.ADDRESS1 />
				<cfset address.ADDRESS2 = arguments.obj.ADDRESS2 />
				<cfset address.CITY = arguments.obj.CITY />
				<cfset address.STATE_PROVINCE = arguments.obj.STATE_PROVINCE />
				<cfset address.POSTAL_CODE = arguments.obj.POSTAL_CODE />
				<cfset address.COUNTRY = arguments.obj.COUNTRY />
				<cfset address.PHONE = arguments.obj.PHONE />
				<cfset address.FAX = arguments.obj.FAX />
				<cfset address.ADDRESS_CAT_ID = arguments.obj.ADDRESS_CAT_ID />
							
				<cfset fooddrive = createObject("component","FoodDrive")>
				<cfset fooddrive.TITLE = arguments.obj.FOODDRIVE_TITLE />
				<cfset fooddrive.DESCRIPTION = arguments.obj.FOODDRIVE_DESC /> 
				<cfset fooddrive.START_DATE = arguments.obj.START_DATE />
				<cfset fooddrive.END_DATE = arguments.obj.END_DATE />
				<cfset fooddrive.GOAL = arguments.obj.GOAL />
	
				<cfif emailCheck.recordcount eq 0>
					
					<!--- save the organization --->
					<cfset organization = createObject("component","OrganizationDAO").create(organization) />				
	
					<!--- save the fooddrive --->
					<cfset fooddrive.ORG_ID = organization.ID />
					<cfset fooddrive = createObject("component","FoodDriveDAO").create(fooddrive) />
	
					<!--- create the address object --->
					<cfset address = createObject("component", "AddressDAO").create(address) />
					
					<!--- save the user --->
					<cfset arguments.obj.USER_ORG_ID = organization.ID />
					<cfset arguments.obj.ADDRESS_ID = address.ID />				
					<cfset user = createObject("component", "UserDAO").create(arguments.obj) />	
					
					<cfset organization.ADMIN_ID = user.ID />
					<cfset organization.addressID = address.ID />
					<cfset organization = createObject("component","OrganizationDAO").update(organization) />									
					
					<cfset user.admin_ID = user.ID>
					
					<cfreturn user>
					
				<cfelse>
				
					<!--- the email address was in use --->
					<cfthrow message="The email address you have provided belongs to another user" type="EMAIL_IN_USE">								
			
				</cfif>		
			
			<cfelse>
			
				<!--- update the user --->
				<cfset user = createObject("component", "UserDAO").update(arguments.obj) />
			
				<!--- update the address --->
				<cfset address = createObject("component", "AddressDAO").update(address) />					
			
				<!--- update the organization --->
				<cfset organization = createObject("component", "OrganizationDAO").update(organization) />		
				
				<cfreturn user>
			</cfif>

	</cffunction>

	<cffunction name="deleteUser" output="false" access="remote">
		<cfargument name="obj" required="true" />
		<cfset createObject("component", "UserDAO").delete(arguments.obj)>
	</cffunction>
	
	<!--- get user by fooddrivID --->
	<cffunction name="GetUserByFoodDriveID" output="false" access="remote">
		<cfargument name="id" required="true" />
		<cfset var qRead = "">
		
		<cfquery username="#request.username#" password="#request.password#" name="qRead" datasource="nwharvest">
			select FoodDrives.ID from FoodDrives
			join Organizations on Organizations.ID = FoodDrives.Org_ID
			where Organizations.Admin_ID = <cfqueryparam cfsqltype="integer" value="#arguments.id#">
		</cfquery>v
		
 		<cfreturn createObject("component", "FoodDriveDAO").read(qRead.ID)>
	</cffunction>
	<!--- get user by email --->
	<cffunction name="getUserIDByEmail" output="false" access="remote">
		<cfargument name="email" required="true" />
		
		<cfquery username="#request.username#" password="#request.password#" name="q_getUserID" datasource="nwharvest">
			select ID from Users
			where email = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#arguments.email#">
		</cfquery>v
		
 		<cfreturn q_getUserID>
	</cffunction>
	<cffunction name="getTotalDonationsByUserID" output="true" access="remote" returnType="numeric">
		<cfargument name="id" required="true" />
		
		<cfquery username="#request.username#" password="#request.password#" name="donations" datasource="nwharvest">
		SELECT ISNULL( SUM( DONATIONS.AMOUNT ), 0 ) as TOTAL
		FROM ( ORGANIZATIONS INNER JOIN FOODDRIVES ON ORGANIZATIONS.ID = FOODDRIVES.ORG_ID ) 
						INNER JOIN DONATIONS ON FOODDRIVES.ID = DONATIONS.FOODDRIVE_ID
		WHERE ORGANIZATIONS.ADMIN_ID = <cfqueryparam cfsqltype="integer" value="#arguments.id#">
		</cfquery>
		
 		<cfreturn donations.TOTAL>
	</cffunction>




</cfcomponent>